package main

import (
	_ "github.com/go-sql-driver/mysql"
)

//database, err := sqlx.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test")
////database, err := sqlx.Open("数据库类型", "用户名:密码@tcp(地址:端口)/数据库名")
//
//type Person struct {
//	UserId   int    `db:"user_id"`
//	Username string `db:"username"`
//	Sex      string `db:"sex"`
//	Email    string `db:"email"`
//}
//
//type Place struct {
//	Country string `db:"country"`
//	City    string `db:"city"`
//	TelCode int    `db:"telcode"`
//}
//
//var Db *sqlx.DB
//
//func init() {
//	database, err := sqlx.Open("mysql", "root:123456qwe@tcp(127.0.0.1:3306)/test")
//	fmt.Println("初始化")
//	if err != nil {
//		fmt.Println("open mysql failed,", err)
//		return
//	}
//	Db = database
//	//defer Db.Close()  // 注意这行代码要写在上面err判断的下面
//}

func main() {
	//插入操作
	//r, err := Db.Exec("insert into person(username, sex, email)values(?, ?, ?)", "华子", "man", "stu01@qq.com")
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	fmt.Println("失败了")
	//	return
	//}
	//id, err := r.LastInsertId()
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	fmt.Println("又失败了")
	//	return
	//}
	//fmt.Println("insert succ:", id)


    //select操作
	//var person []Person
	//err := Db.Select(&person, "select user_id, username, sex, email from person where user_id=?", 3)
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//fmt.Println("select succ:", person)
	//




	////update功能
	//res, err := Db.Exec("update person set username=? where user_id=?", "张华", 2)
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//row, err := res.RowsAffected()
	//if err != nil {
	//	fmt.Println("rows failed, ",err)
	//}
	//fmt.Println("update succ:",row)



	//删除操作
	//res, err := Db.Exec("delete from person where user_id=?", 4)
	//if err != nil {
	//	fmt.Println("exec failed, ", err)
	//	return
	//}
	//
	//row,err := res.RowsAffected()
	//if err != nil {
	//	fmt.Println("rows failed, ",err)
	//}
	//
	//fmt.Println("delete succ: ",row)
}
